package com.cn.springcloudwechat.controller.articleAndMenu;


import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.model.YmAnswer;
import com.cn.springcloudwechat.model.YmPraise;
import com.cn.springcloudwechat.model.articleAndMenu.YmActivityArticle;
import com.cn.springcloudwechat.model.articleAndMenu.YmArticle;
import com.cn.springcloudwechat.model.articleAndMenu.YmArticleExpand;
import com.cn.springcloudwechat.model.articleAndMenu.YmScrollArticle;
import com.cn.springcloudwechat.model.find.communication.YmAnswerVO;
import com.cn.springcloudwechat.service.YmAnswerService;
import com.cn.springcloudwechat.service.articleAndMenu.YmArticleService;
import com.cn.springcloudwechat.service.find.communication.CommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章相关Controller
 *
 * @author: Administrator
 * @create: 2018/7/20 16:51
 */
@RestController
@RequestMapping("/web/article/")
public class YmArticleControllerWeb {
    @Autowired
    YmArticleService ymArticleService;
    @Autowired
    YmAnswerService ymAnswerService;
    @Autowired
    CommunicationService communicationService;
    private static YmPraise ymPraise = new YmPraise();
    private static String YMPRAISE_TYPE = "1";
    private static int ANSWER_TYPE = 1;

    /**
     * 添加文章
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @RequestMapping(value = "insertArticle", method = RequestMethod.POST)
    public AjaxRes insertArticle(AjaxRes ajaxRes, YmArticle record) {
        Long articleId = ymArticleService.insertArticle(record);
        ajaxRes.setSucceed(articleId);
        return ajaxRes;
    }

    /**
     * 更新文章
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @RequestMapping(value = "updateArticle", method = RequestMethod.POST)
    public AjaxRes updateArticle(AjaxRes ajaxRes, YmArticle record) {
        int num = ymArticleService.updateArticle(record);
        ajaxRes.setSucceed(num);
        return ajaxRes;
    }

    /**
     * 获取推荐文章
     *
     * @param ajaxRes
     * @return
     */
    @RequestMapping(value = "getRecommendMenuArticle", method = RequestMethod.GET)
    public AjaxRes getRecommendMenuArticle(AjaxRes ajaxRes) {
        Map<String, Object> ymArticles = ymArticleService.getRecommendMenuArticle();
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }

    /**
     * 获取相关类型文章
     *
     * @param ajaxRes
     * @param articleType
     * @return
     */
    @RequestMapping(value = "getYmArticle", method = RequestMethod.GET)
    public AjaxRes getYmArticle(AjaxRes ajaxRes, @RequestParam Integer articleType, Page<YmArticle> page) {
        Page<YmArticle> ymArticles = ymArticleService.getYmArticle(articleType, page);
        //查询回复量及阅读量
        if (ymArticles.getResults() != null) {
            for (YmArticle ymArticle : ymArticles.getResults()) {
                getYmArticleExpand(ymArticle);
            }
        }
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }

    /**
     * 获取相关类型文章
     *
     * @param ajaxRes
     * @param articleType
     * @return
     */
    @GetMapping(value = "getYmArticlePagination")
    public AjaxRes getYmArticlePagination(AjaxRes ajaxRes, Integer articleType, Page<YmArticle> page) {
        Page<YmArticle> ymArticles = ymArticleService.getYmArticle(articleType, page);
        //查询回复量及阅读量
        if (ymArticles.getResults() != null) {
            for (YmArticle ymArticle : ymArticles.getResults()) {
                getYmArticleExpand(ymArticle);
            }
        }
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }

    /**
     * 获取单个文章数据
     *
     * @param ajaxRes
     * @param articleId
     * @return
     */
    @RequestMapping(value = "getYmArticleByArticleId", method = RequestMethod.GET)
    public AjaxRes getYmArticleByArticleId(AjaxRes ajaxRes, @RequestParam Long articleId) {
        YmArticle ymArticle = ymArticleService.selectByPrimaryKey(articleId);
        getYmArticleExpand(ymArticle);
        ajaxRes.setSucceed(ymArticle);
        return ajaxRes;
    }

    /**
     * 根据轮播状态获取轮播文章
     *
     * @param ajaxRes
     * @param scrollType
     * @return
     */
    @RequestMapping(value = "getScrollArticle", method = RequestMethod.GET)
    public AjaxRes getScrollArticle(AjaxRes ajaxRes, @RequestParam Integer scrollType) {
        List<YmScrollArticle> ymArticles = ymArticleService.getScrollArticle(scrollType);
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }


    /**
     * 更新轮播图状态
     *
     * @param ajaxRes
     * @param scrollArticleId
     * @param state
     * @return
     */
    @RequestMapping(value = "updateScollArticleState", method = RequestMethod.POST)
    public AjaxRes updateScollArticleState(AjaxRes ajaxRes, @RequestParam("scrollArticleId") List<Long> scrollArticleId, short state) {
        ymArticleService.updateScollArticleState(scrollArticleId, state);
        ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        return ajaxRes;
    }


    /**
     * 添加轮播文章
     *
     * @param ajaxRes
     * @param ymScrollArticle
     * @return
     */
    @RequestMapping(value = "insertScrollArticle", method = RequestMethod.POST)
    public AjaxRes insertScrollArticle(AjaxRes ajaxRes, YmScrollArticle ymScrollArticle) {
        Long scrollArticleId = ymArticleService.insertScrollArticle(ymScrollArticle);
        ajaxRes.setSucceed(scrollArticleId);
        return ajaxRes;
    }

    /**
     * 更新轮播文章
     *
     * @param ajaxRes
     * @param ymScrollArticle
     * @return
     */
    @RequestMapping(value = "updateScrollArticle", method = RequestMethod.POST)
    public AjaxRes updateScrollArticle(AjaxRes ajaxRes, YmScrollArticle ymScrollArticle) {
        int num = ymArticleService.updateScrollArticle(ymScrollArticle);
        ajaxRes.setSucceed(num);
        return ajaxRes;
    }


    /**
     * 根据条件获取轮播图
     *
     * @param ajaxRes
     * @param ymScrollArticle
     * @param page
     * @return
     */
    @RequestMapping(value = "getScrollArticleByParameter", method = RequestMethod.GET)
    public AjaxRes getScrollArticleByParameter(AjaxRes ajaxRes, YmScrollArticle ymScrollArticle, Page<Object> page) {
        Page<Object> ymScrollArticles = ymArticleService.getScrollArticleByParameter(ymScrollArticle, page);
        ajaxRes.setSucceed(ymScrollArticles);
        return ajaxRes;
    }


    /**
     * 通过菜单ID 获取文章
     *
     * @param ajaxRes
     * @param menuTagId
     * @return
     */
    @RequestMapping(value = "getMenuArticle", method = RequestMethod.GET)
    public AjaxRes getMenuArticle(AjaxRes ajaxRes, @RequestParam Long menuTagId, Page<YmArticle> page) {
        //menuTagId  为0 查询公告
        Page<YmArticle> ymArticles = ymArticleService.getMenuArticle(menuTagId, page);
        //查询回复量及阅读量
        if (ymArticles.getResults() != null) {
            for (YmArticle ymArticle : ymArticles.getResults()) {
                getYmArticleExpand(ymArticle);
            }
        }
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }

    /**
     * 查询文章点赞数及阅读量
     *
     * @param ajaxRes
     * @param articleId
     * @return
     */
    @RequestMapping(value = "getArticleExpandData", method = RequestMethod.GET)
    public AjaxRes getArticleExpandData(AjaxRes ajaxRes, @RequestParam Long articleId) {
        YmArticleExpand ymArticleExpand = ymArticleService.getArticleExpandData(articleId);
        ajaxRes.setSucceed(ymArticleExpand);
        return ajaxRes;
    }

    /**
     * 更新文章阅读量
     *
     * @param ajaxRes
     * @param articleId
     * @return
     */
    @RequestMapping(value = "updateArticleReadNumber", method = RequestMethod.POST)
    public AjaxRes updateArticleReadNumber(AjaxRes ajaxRes, @RequestParam Long articleId) {
        ymArticleService.updateArticleReadNumber(articleId);
        ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 添加活动
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @RequestMapping(value = "insertSelective", method = RequestMethod.POST)
    public AjaxRes insertSelective(AjaxRes ajaxRes, YmActivityArticle record) {
        ymArticleService.insertSelective(record);
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 获取活动文章
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @RequestMapping(value = "getActivityArticle", method = RequestMethod.GET)
    public AjaxRes getActivityArticle(AjaxRes ajaxRes, YmActivityArticle record) {
        List<YmActivityArticle> ymActivityArticles = ymArticleService.getActivityArticle(record);
        ajaxRes.setSucceed(ymActivityArticles);
        return ajaxRes;
    }

    /**
     * 更新活动文章
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @RequestMapping(value = "updateActivityArticle", method = RequestMethod.POST)
    public AjaxRes updateActivityArticle(AjaxRes ajaxRes, YmActivityArticle record) {
        ymArticleService.updateActivityArticle(record);
        ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 主贴回复列表
     *
     * @param ajaxRes
     * @param articleId
     * @param type
     * @return
     */
    @GetMapping(value = "getYmAnswerList")
    public AjaxRes queryYmAnswerList(AjaxRes ajaxRes, @RequestParam(value = "typeId") Long articleId, Integer type,
                                     @RequestParam(value = "praiseType", required = false) String praiseType) {
        List<YmAnswerVO> ymAnswerVOs = null;
        Long replyCount = 0L;
        int praise = 0;
        Map<String, Object> resultMap = new HashMap<String, Object>(3);

        if (type == null || type == 0) {
            ymAnswerVOs = communicationService.queryYmAnswerList(articleId, ANSWER_TYPE);
            replyCount = ymAnswerService.getAnswerQuantity(articleId, ANSWER_TYPE);
            ymPraise.setPraiseType(YMPRAISE_TYPE);
        } else {
            ymAnswerVOs = communicationService.queryYmAnswerList(articleId, type);
            replyCount = ymAnswerService.getAnswerQuantity(articleId, type);
            ymPraise.setPraiseType(praiseType);
        }
        ymPraise.setPraiseTypeId(articleId);
        ymPraise.setUserId(ThreadLocalUtil.getUser().getUserId());
        Integer state = communicationService.praiseExist(ymPraise);
        praise = communicationService.praiseCount(ymPraise);
        resultMap.put("ymAnswerVOs", ymAnswerVOs);
        resultMap.put("replyCount", replyCount);
        resultMap.put("praiseCount", praise);
        resultMap.put("praiseState", null == state ? 0 : state);
        ajaxRes.setSucceed(resultMap);
        return ajaxRes;
    }

    /**
     * 帖子回复详情
     *
     * @param ajaxRes
     * @param articleId
     * @param type
     * @return
     */
    @GetMapping(value = "getYmAnswerDetail")
    public AjaxRes queryYmAnswerDetail(AjaxRes ajaxRes, @RequestParam(value = "typeId") Long articleId, Integer type) {
        List<YmAnswerVO> ymAnswerVOs = null;
        Long replyCount = 0L;
        int praise = 0;
        Map<String, Object> resultMap = new HashMap<String, Object>(1);
        if (type == null || type == 0) {
            ymAnswerVOs = communicationService.queryYmAnswerDetail(articleId, ANSWER_TYPE);
        } else {
            ymAnswerVOs = communicationService.queryYmAnswerDetail(articleId, type);
        }
        resultMap.put("ymAnswerVOs", ymAnswerVOs);
        ajaxRes.setSucceed(resultMap);
        return ajaxRes;
    }

    /**
     * 添加回复
     *
     * @param ajaxRes
     * @param ymAnswer
     * @param type
     * @return
     */
    @PostMapping(value = "insertAnser")
    public AjaxRes insertAnser(AjaxRes ajaxRes, YmAnswer ymAnswer, Integer type) {
        ymAnswer.setAnswerUserId(ThreadLocalUtil.getUser().getUserId());
        if (type == null || type == 0) {
            communicationService.insertAnser(ymAnswer, ANSWER_TYPE);
        } else {
            communicationService.insertAnser(ymAnswer, type);
        }
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 更新文章状态
     *
     * @param ajaxRes
     * @param articleId
     * @param state
     * @return
     */
    @PostMapping(value = "updateArticleState")
    public AjaxRes updateArticleState(AjaxRes ajaxRes, @RequestParam("articleId") List<Long> articleId, short state) {
        ymArticleService.updateArticleState(articleId, state);
        ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 根据条件获取文章
     *
     * @param ajaxRes
     * @param ymArticle
     * @param page
     * @return
     */
    @GetMapping(value = "getArticlesParameter")
    public AjaxRes getArticlesParameter(AjaxRes ajaxRes, YmArticle ymArticle, Page<Object> page) {
        Page<Object> ymArticles = ymArticleService.getArticlesParameter(ymArticle, page);
        ajaxRes.setSucceed(ymArticles);
        return ajaxRes;
    }


    /**
     * 设置回复数量 阅读点赞
     *
     * @param ymArticle
     */
    private void getYmArticleExpand(YmArticle ymArticle) {
        Long articleId = ymArticle.getArticleId();
        YmArticleExpand ymArticleExpand = ymArticleService.getArticleExpandData(articleId);
        if (ymArticleExpand == null) {
            ymArticle.setPraiseNumer(0L);
            ymArticle.setReadNumber(0L);
        } else {
            ymArticle.setPraiseNumer(ymArticleExpand.getPraiseNumer());
            ymArticle.setReadNumber(ymArticleExpand.getReadNumber());
        }
        Long replyCount = ymAnswerService.getAnswerQuantity(articleId, ANSWER_TYPE);
        if (replyCount == null) {
            ymArticle.setReplyCount(0L);
        } else {
            ymArticle.setReplyCount(replyCount);
        }
    }


}
